package com.zhiyou100.loans.service.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Calendar;
import java.util.Date;

import com.zhiyou100.loans.dao.LoanbillDao;
import com.zhiyou100.loans.dao.RefundbillDao;
import com.zhiyou100.loans.dao.impl.LoanbillDaoImpl;
import com.zhiyou100.loans.dao.impl.RefundbillDaoImpl;
import com.zhiyou100.loans.model.Loanbill;
import com.zhiyou100.loans.model.Refund;
import com.zhiyou100.loans.model.Result;
import com.zhiyou100.loans.service.RefundbillService;
import com.zhiyou100.loans.util.NetDate;
import com.zhiyou100.loans.util.StringArrayUtil;

import net.sf.json.JSONObject;

public class RefundbillServiceImpl implements RefundbillService {
	RefundbillDao rd = new RefundbillDaoImpl();//还款单dao
	LoanbillDao ld = new LoanbillDaoImpl();//借款单dao
	@Override
	public JSONObject add(String lbid) {
		//根据借款单id，查询总期数(分成多少条记录)，借款金额(计算每期的还款金额)，截止时间(推算每一期的还款时间)
		Loanbill lb = getLoanbill(Integer.parseInt(lbid));
		int months = lb.getDuration();//总期数
		double money = lb.getMoney();
		double monthrate = lb.getMonthrate();
		//计算每月的还款金额Aβ(1+β)^m /[(1+β)^m - 1],等额本息法
		double monthmoney = money*monthrate*Math.pow(1+monthrate, months)/(Math.pow(1+monthrate, months)-1);
		//对monthMoney进行四舍五入保留2位小数
		monthmoney=((int)((monthmoney+0.005)*100))/100.0;
		Date end = lb.getSuccendtime();
		int[] as = new int[months];
		Calendar cal = Calendar.getInstance();
		for(int i=1;i<=months;i++){
			//可以使用statment对象的批处理方法，addBatch();executeBatch();
			//对日期进行处理,通过日历类来完成月份的加减
			cal.setTime(end);
			//计算时间
			cal.add(Calendar.MONTH, i-months);//11+(1-12)
			//转换为日期
			Date monthend=cal.getTime();
			as[i-1]=rd.add(Integer.parseInt(lbid),monthmoney,monthend,i);
		}
		Result result = new Result(false, "添加还款记录失败");
		if(!(StringArrayUtil.arrayToString(as, "-").contains("0"))){
			result.setFlag(true);
			result.setMessage("添加还款记录成功");
		}
		return JSONObject.fromObject(result);
	}
	
	public Loanbill getLoanbill(int lbid){
		ResultSet rs=ld.selectMonthByLbid(lbid);
		Loanbill lb = new Loanbill();
		try {
			if(rs.next()){
				lb.setDuration(rs.getInt("duration"));
				lb.setMoney(rs.getDouble("money"));
				lb.setSuccendtime(rs.getTimestamp("succendtime"));
				lb.setMonthrate(rs.getDouble("monthrate"));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return lb;
	}

	@Override
	public JSONObject selectByLbid(String loanbillid) {
		Calendar cal = Calendar.getInstance();
		Date date= NetDate.getCurrentTime();
		cal.setTime(date);
		cal.add(Calendar.DATE, NetDate.getDays(date));
		Date date2 = cal.getTime();
		ResultSet rs = rd.selectByLbid(Integer.parseInt(loanbillid),date2);
		Refund refund = null;
		try {
			while(rs.next()){
				refund = new Refund();
				refund.setId(rs.getInt("id"));
				refund.setMoney(rs.getDouble("money"));
				refund.setMonths(rs.getInt("months"));
				
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return JSONObject.fromObject(refund);
	}

	@Override
	public JSONObject update(String id) {
		int i=rd.update(Integer.parseInt(id),NetDate.getCurrentTime());
		Result result = new Result(false, "还款失败");
		if(i>0){
			result.setFlag(true);
			result.setMessage("还款成功");
		}
		return JSONObject.fromObject(result);
	}

}
